# -*- coding: utf-8 -*-


import  requests
import json
import sys

reload(sys)

sys.setdefaultencoding('utf8')

url_domain = 'http://longtan.xxx.cn/controller4ApiService/getDomainAndProjectByEnvType.json'
url_project = 'http://longtan.xxx.cn/controller4ApiService/getProjectInfoByEnv.json'

header = {'cache-control':'no-cache','postman-token':'f2b966a-acfb-3781-d2ef-07e713402426',
          'x-requested-with':'XMLHttpRequest'
          }
params = {'env':'qa'}

#打开文件
fc = open('./c.txt','w')
fb = open('./b.txt','w')
fh = open('./h.txt','w')
fclt = open('./clt.txt','a')
fcdcb = open('./cdcb.txt','a')
fcdcc = open('./mcdcc.txt','a')
fmc = open('./mc.txt','a')
fhpo = open('./hpo.txt','w')
fxy = open('./xy.txt','a')
flbb = open('./lbb.txt','w')
fduomian = open('./duomian.txt','w')
fnocookie = open('./nocookie.txt','w')
#获取域名和项目对应关系
def getdomain():
    domain_dict = {}
    try:
        response = requests.post(url_domain, params=params, headers=header)
        res = response.content.decode('utf8')
        resdata = json.loads(res)['data']
        print(res)
        for i in resdata:
            #print(i['dns_domain'],i['project_names'])
            domain_dict[i['dns_domain']] = i['project_names']
        return domain_dict
    except Exception as e:
        print(str(e))
        return

#获取所有项目和项目所属组对应信息
def getproject():
    project_dict = {}
    try:
        response = requests.post(url_project,params=params,headers=header)
        res = response.content.decode('utf8')
        print(res)
        resdata = json.loads(res)['data']
        #print(type(resdata['data']))
        for i in resdata:
            project_dict[i['project_name']] = i['pg_name']
            #print(i['pg_name'])
        return project_dict
    except Exception as e:
        print(str(e))
        return

# print(domain_dict)
# print(project_dict)
#整理域名和所属组对应关系
def domain2pgname():
    domain2pgname = {}
    for key in domain_dict:
        for j in project_dict:
            if domain_dict[key] == j:
                #print(key,project_dict[j])
                domain2pgname[key] = project_dict[j]
    return domain2pgname

# domain_dict = getdomain()
# project_dict = getproject()
# domain2pgname_dict = domain2pgname()




def getdomains():
    for key in domain2pgname_dict:
        if 'xxx.com' in key and domain2pgname_dict[key] == 'C端业务部':
            #print(key)
            fc.write('https://'+key+'\n')
        elif 'xxx.com' in key and domain2pgname_dict[key] == '付费机构产品研发部':
            fb.write('https://'+key+'\n')
        elif 'xxx.com' in key:
            if domain2pgname_dict[key] == '交易产品研发部' or domain2pgname_dict[key] == '商业产品研发部':
                fh.write('https://'+key+'\n')
        elif 'xxx.com' in key:
            flbb.write('https://'+key+'\n')
        elif 'xxx.cn' in key and key != 'longtan.xxx.cn':
            fhpo.write('http://'+key+'\n')
        elif 'xxx.cn' in key or 'xxx.com' in key or 'xxx.com' in key:
            fduomian.write('https://'+key+'\n')
        else:
            fnocookie.write('https://'+key+'\n')

# for i in f.readlines():
domain_dict = getdomain()
project_dict = getproject()
domain2pgname_dict = domain2pgname()
#print(project_dict)
#print(domain2pgname_dict)

# for key in domain2pgname_dict:
#     if 'liepin.com' in key and  domain2pgname_dict[key] == '校园':
#         print(key)


#循环关闭文件

def closefile():
    fc.close()
    fb.close()
    fh.close()
    flbb.close()
    fhpo.close()
    fduomian.close()
    fxy.close()
    fcdcb.close()
    fcdcc.close()
    fnocookie.close()
    fclt.close()
    fmc.close()

getdomains()
closefile()
#测试循环输出
# for key in domain2pgname_dict:
#     print(key,domain2pgname_dict[key])



